<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: HCZ <huangchengze@etrol.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller;

use app\common\controller\Admin;
use app\basic\controller\api\Enterprise as EnterpriseApi;
use app\admin\controller\ajax\Library as LibraryAjax;
use app\basic\controller\api\Route;
use lib\ExcelToArrary;
/**
 * 企业管理
 * @package app\enterprise
 */
class Enterprise  extends Admin

{
    
    protected $api;
    protected $property;
    
	public function _initialize()
	{
		parent::_initialize();
		$this->api = new EnterpriseApi();
		$this->property = new LibraryAjax();
		$this->excel=new ExcelToArrary();
	}
    /**
     * 首页
     *
     *
     */
   	public function index() {
   	  if(IS_AJAX){
   	  	$enterpriseList = $this->api->enterpriseList($this->scope);
   	  	return $enterpriseList;
   	  }
   	  $positionList = $this->property->getPositionSelect();
   	  $this->assign('positionList',$positionList->getData());
      return $this->fetch();
 	}

 	/**
 	 * 查询
 	 */
 	public function search() {
 		if(IS_AJAX){
 			$enterpriseList = $this->api->search($this->scope,input('areaId'),input('fullName'));
 			return $enterpriseList;
 		}
 	}
    /**
     * 添加企业
     * 
     */
    public function add() {
   		 if(IS_POST){
   		 	$data = input('post.');
   		 	if($data['position-area']){
    	  		list($data['positionId'],$data['areaId']) = explode('-',$data['position-area']);
    	  	}
    	  	unset($data['position-area']);
   		 	return $this->api->add($data);
   		 }
   		 $positionType = $this->property->getPositionType()->getData();
   		 foreach($positionType as &$item){
   		 	$item['area'] = $this->property->getAreaType($item['subsystemNo'])->getData();
   		 }
   		 $this->assign('positionType',$positionType);
   		 $productionStatus = $this->property->getProductionStatusType()->getData();
   		 $this->assign('productionStatus',$productionStatus);
   		 $enterpriseType = $this->property->getEnterpriseType()->getData();
   		 $this->assign('enterpriseType',$enterpriseType);

         return $this->fetch();    
     }
 
    /**
     * 修改企业
     * 
     */
    public function edit($enterpriseId = null) {
    	  if(IS_POST){
    	  	$data = input('post.');
    	  	if($data['position-area']){
    	  		list($data['positionId'],$data['areaId']) = explode('-',$data['position-area']);
    	  	}
    	  	unset($data['position-area']);
    	  	return $this->api->edit($data);
    	  }
     	  $enterpriseList = $this->api->enterpriseList($enterpriseId)->getData();
     	  $this->assign('info',$enterpriseList['data'][0]);
     	  $positionType = $this->property->getPositionType()->getData();
     	  foreach($positionType as &$item){
     	  	$item['area'] = $this->property->getAreaType($item['subsystemNo'])->getData();
     	  }
     	  $this->assign('positionType',$positionType);
		  $productionStatus = $this->property->getProductionStatusType()->getData();
		  $this->assign('productionStatus',$productionStatus);

     	  $enterpriseType = $this->property->getEnterpriseType()->getData();
     	  $this->assign('enterpriseType',$enterpriseType);
          return $this->fetch();      
    }
    
    /**
     * 删除企业
     */
    public function del() {
    	if(IS_AJAX){
    		return $this->api->del();
    	}
    }


	/**
	 * Excel导入企业
	 */
	public function  import(){
		//保存文件路径
		$info='';
		$path = input('path');
		$path = './public/upload/'.$path;
		$file = request()->file('file');
		if(!file_exists($path)){
			mkdir ($path,0777,true);
		}
		if($file){
			$info = $file->move($path, true, false);
			$path = substr(str_replace("\\", '/', substr($info->getPathname(), 1)), 7) ;
			//文件类型
			$fileType=substr($path,strrpos($path,'.')+1);
			//路径
			$path='public/'.$path;

		}

		if ($info) {

			$res=$this->excel->read($path ,"UTF-8",$fileType);

			$count=count($res);
			$data=array();
			for($x=2;$x<=$count;$x++){
				$data[$x]['enterpriseCode'] = $res[$x][0];
				$data[$x]['fullName'] = $res[$x][1];
				$data[$x]['shortname'] = $res[$x][2];
				$data[$x]['positionId'] = $res[$x][3];
				$data[$x]['areaId'] = $res[$x][4];
				$data[$x]['latitudeLongitude'] = $res[$x][5];
				$data[$x]['enterDate'] = $res[$x][6];
				$data[$x]['productionStatus'] = $res[$x][7];
				$data[$x]['typeId'] = $res[$x][8];
				$data[$x]['totalInvestment'] = $res[$x][9];
				$data[$x]['outPutValue'] = $res[$x][10];
				$data[$x]['outPutAmount'] = $res[$x][11];
				$data[$x]['unitId'] = $res[$x][12];
				$data[$x]['productDesc'] = $res[$x][13];
				$data[$x]['legalPerson'] = $res[$x][14];
				$data[$x]['generalManager'] = $res[$x][15];
				$data[$x]['tel'] = $res[$x][16];
				$data[$x]['mobile'] = $res[$x][17];
				$data[$x]['email'] = $res[$x][18];
				$data[$x]['IsInPark'] = $res[$x][19];

			}

			$result=model('enterprise')->saveAll($data);
			/*对生成的数组进行数据库的写入*/
		if($result){
				return json(['code'=>1,'msg'=>'企业信息导入成功']);
				}
			    } else {
			return json(['code'=>0,'msg'=>'企业信息导入失败']);
		    }
		}

    /**
     * 设置建筑位置
     * @param string $position
     * @param string $enterprise
     * @return mixed
     */
    public function setBuilding($position = null,$enterprise = null){
        $center = $this->api->getEnterprisePosition($enterprise)->getData();
        $center = $center['code']?$center['data']:null;
        $this->assign('center',$center);
		$this->assign('position',$position);
		return $this->fetch();
    }
    
    /**
     * 申请审核
     * @param string $id
     */
    public function audit($id = null){
    	return $this->api->audit($id);
    }
    
    /**
     * 逃生路径
     * @param string $enterpriseId 企业ID
     * @return mixed
     */
    public function route($enterpriseId = NULL){
    	if(IS_POST){
    		$routeApi = new Route();
    		return $routeApi->add(input('post.'));
    	}
    	$enterprise = model('enterprise')->find($enterpriseId);
    	$routes = json_encode($enterprise->routes);
    	$this->assign('enterprise',$enterprise);
    	$this->assign('routes',$routes);
    	return $this->fetch();  	
    }
    
    /**
     * 删除路径
     * @param string $name 路径名称
     * @param $enterprise 企业ID
     */
    public function deleteRoute(){
    	if(IS_AJAX){
    		$routApi = new Route();
    		return $routApi->del(input('name'), input('enterpriseId'));
    	}
    }

}